﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Configuration;

using chad.azure.diagnostics.data;
using chad.azure.diagnostics.wad;
using chad.azure.diagnostics.export;
using chad.azure.diagnostics.excel;

using System.IO;

namespace azure.diagnostics.reporter.console
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                //DEBUG:
                //args = new string[2];
                //args[0] = "/config";
                //args[1] = @"C:\Scratch\wad_reporter_config.json";

                // Verify the configuration parameter
                if (args.Contains("/config"))
                {
                    if (args.Count() != 2)
                    {
                        ConsoleWriteHelp(String.Format("Two parameters expected for '/config', '{0}' parameters found.", args.Count()));
                    }

                    MainJsonConfig(args);
                }
                else if (args.Contains("/makedefaultconfig"))
                {
                    if (args.Count() != 2)
                    {
                        ConsoleWriteHelp(String.Format("Two parameters expected for '/makedefaultconfig', '{0}' parameters found.", args.Count()));
                    }

                    using (StreamWriter writer = new StreamWriter(args[1]))
                    {
                        writer.Write(ConfigurationFileProcessor.GenerateReportConfigurationTemplate());
                    }
                }
                else
                {
                    throw new Exception("Valid parameters not found");
                }
            } catch (Exception ex)
            {
                ConsoleWriteHelp(ex.ToString());
                Environment.Exit(-1);
            }
        }

        private static void TryWriteFileFailToConsoleAndExit(String fullFilePath)
        {
            try
            {
                File.WriteAllText(fullFilePath, "Test");
                File.Delete(fullFilePath);
            } 
            catch (Exception ex)
            {
                ConsoleWriteHelp(String.Format("Failed to create file '{0}' with exception: {1}",fullFilePath,ex.ToString()));

                Environment.Exit(-1);
            }
        }

        private static void MainJsonConfig(string[] args)
        {
            // Read the config
            ReportConfiguration config = ConfigurationFileProcessor.GetReportConfigurationFromJsonFile(args[1]);
            // Create the report
            String fileNameAndPath = ExcelReportBuilder.GenerateReportFromConfiguration(config);

            // Sent in an email
            EmailProcessor.SendEmail(config.ReportOutput.SmtpServerName, config.ReportOutput.SmtpServerPort,
                config.ReportName, " ",
                config.ReportOutput.FromEmailAddess, config.ReportOutput.ToEmailAddresses,
                fileNameAndPath);
            
            // Delete the file if true
            if(config.ReportOutput.DeleteFileAfterSendingEmail)
            {
                File.Delete(fileNameAndPath);
            }
        }


        private static void ConsoleWriteHelp(String errorMessage = null)
        {
            if(errorMessage != null && errorMessage != String.Empty)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(errorMessage);
                Console.WriteLine();
                Console.ResetColor();
            }

            Console.WriteLine("USAGE:");
            Console.WriteLine("    azure.diagnostics.reporter.console");
            Console.WriteLine("            /config [full_file_location]");
            Console.WriteLine("            /makedefaultconfig [full_file_location]");
            Console.WriteLine("");
            Console.WriteLine("    Options:");
            Console.WriteLine(@"        /config"+"\t"+"Run the Azure Diagnostics report using settings in the specified configuration file");
            Console.WriteLine(@"        /config"+"\t"+"Create a default report configuration file");
            Console.WriteLine("");
            Console.WriteLine("    Examples:");
            Console.WriteLine(@"        /config c:\files\myfile.json");
            Console.WriteLine(@"        /makedefaultconfig c:\files\myfile.json");
        }

    }
}
